package cn.danger.com.view.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;

import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.datatype.BmobDate;
import cn.danger.com.activity.R;
import cn.danger.com.model.entity.Schedule;
import cn.danger.com.presenter.SchedulePresenter;
import cn.danger.com.util.DateUtil;
import cn.danger.com.util.FileCacheService;

/**
 * Created by 俊 on 2017/1/4.
 */
public class ScheduleAdapter extends BaseSwipeAdapter {

    private Context context;
    private List<Schedule> list;
    private SchedulePresenter presenter;
    private int work_color;
    private int route_color;
    private int life_color;

    public ScheduleAdapter(Context context, List<Schedule> list, SchedulePresenter presenter) {
        this.context = context;
        this.list = list;
        this.presenter = presenter;
        initColor(context);
    }

    private void initColor(Context context) {
        int[] colors = new int[12];
        colors[0] = context.getResources().getColor(R.color.color1);
        colors[1] = context.getResources().getColor(R.color.color2);
        colors[2] = context.getResources().getColor(R.color.color3);
        colors[3] = context.getResources().getColor(R.color.color4);
        colors[4] = context.getResources().getColor(R.color.color5);
        colors[5] = context.getResources().getColor(R.color.color6);
        colors[6] = context.getResources().getColor(R.color.color7);
        colors[7] = context.getResources().getColor(R.color.color8);
        colors[8] = context.getResources().getColor(R.color.color9);
        colors[9] = context.getResources().getColor(R.color.color10);
        colors[10] = context.getResources().getColor(R.color.color11);
        colors[11] = context.getResources().getColor(R.color.color12);
        Map<String, Object> map = (Map<String, Object>) new FileCacheService(context).getSharePreference("setting");
        work_color = colors[(int) map.get("work_color") - 1];
        route_color = colors[(int) map.get("route_color") - 1];
        life_color = colors[(int) map.get("life_color") - 1];
    }

    @Override
    public int getSwipeLayoutResourceId(int i) {
        return R.id.swipe;
    }

    @Override
    public View generateView(final int position, ViewGroup parent) {
        final View convertView = LayoutInflater.from(context).inflate(R.layout.schedule_listview_item, parent, false);
        final SwipeLayout swipeLayout = (SwipeLayout) convertView.findViewById(getSwipeLayoutResourceId(position));
        final TextView content_textView = (TextView) convertView.findViewById(R.id.content_textView);
        final TextView remind_time_textView = (TextView) convertView.findViewById(R.id.remind_time_textView);
        convertView.findViewById(R.id.delete_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.deleteSchedule(list.get(position));
                swipeLayout.close();
            }
        });
        ((AppCompatCheckBox) convertView.findViewById(R.id.remind_check_box)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Schedule schedule = list.get(position);
                if (Long.parseLong(schedule.getDate_start()) > System.currentTimeMillis()) {
                    if (isChecked) {
                        presenter.cancelAlarm(list.get(position));
                        content_textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                        remind_time_textView.setText("不提醒");
                        schedule.setRemind(2);
                        presenter.updateSchedule(schedule);
                    } else {
                        presenter.sendAlarm(list.get(position));
                        content_textView.getPaint().setFlags(NO_SELECTION);
                        remind_time_textView.setText(list.get(position).getRemind_time() + "提醒");
                        schedule.setRemind(1);
                        presenter.updateSchedule(schedule);
                    }
                }
            }
        });
        return convertView;
    }

    @Override
    public void fillValues(int i, View convertView) {
        CardView cardView = (CardView) convertView.findViewById(R.id.cardView);
        TextView content_textView = (TextView) convertView.findViewById(R.id.content_textView);
        TextView time_textView = (TextView) convertView.findViewById(R.id.time_textView);
        TextView remind_time_textView = (TextView) convertView.findViewById(R.id.remind_time_textView);
        AppCompatCheckBox remind_check_box = (AppCompatCheckBox) convertView.findViewById(R.id.remind_check_box);
        Schedule schedule = list.get(i);
        content_textView.setText(schedule.getContent());
        if (Long.parseLong(schedule.getDate_start()) - DateUtil.getRemindTime(schedule.getRemind_time()) > System.currentTimeMillis()) {
            if (schedule.getRemind() == 1) {
                remind_check_box.setChecked(false);
                remind_time_textView.setText(schedule.getRemind_time() + "提醒");
            } else {
                remind_check_box.setChecked(true);
                remind_time_textView.setText("不提醒");
            }
        } else {
            remind_check_box.setChecked(true);
            remind_time_textView.setText("已提醒");
            content_textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            remind_check_box.setEnabled(false);
        }
        String date = new BmobDate(new Date(Long.parseLong(schedule.getDate_start()))).getDate().split(" ")[1];
        Log.i("danger", "time-->>" + date);
        time_textView.setText(date.substring(0, date.length() - 3));
        if (schedule.getType().equals("工作")) {
            cardView.setCardBackgroundColor(work_color);
        } else if (schedule.getType().equals("行程")) {
            cardView.setCardBackgroundColor(route_color);
        } else if (schedule.getType().equals("生活")) {
            cardView.setCardBackgroundColor(life_color);
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
